#define _CRT_SECURE_NO_WARNINGS

#include"Binary.h"


struct TreeNode {
    int val;
    struct TreeNode* left;
   struct TreeNode* right;
};


struct TreeNode* sb5(int x)
{
    struct TreeNode* tmp = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    if (tmp == NULL)
        exit(-1);
    tmp->val = x;
    tmp->left = NULL;
    tmp->right = NULL;
}



void _tree2str(struct TreeNode* root, int* n, int* size, char* tmp)
{
    if (root == NULL)
        return;
    char a[40] = { '\0' };
    char* p = a;
    int i = 0;
    sprintf(p, "%d", root->val);
    i = strlen(a);
    if (root->left)
        a[i++] = '(';
    if (root->left == NULL && root->right)
        a[i++] = '(';
    /*if(root->left==NULL&&root->right==NULL)
    {
        a[i-1]=')';
    }
    else if(root->left==NULL&&root->right)
    {
        a[i++]=')';
    }*/
    a[i++] = '\0';
    if (*size + i >= *n)
    {
        *n = *n * 2;
        tmp = realloc(tmp, sizeof(char) * (*n));
    }
    strcat(tmp, a);
    *size = *size + i - 1;
    _tree2str(root->left, n, size, tmp);
    if (root->left)
        tmp[(*size)++] = ')';
    if (root->left == NULL && root->right)
        tmp[(*size)++] = ')';
    if (root->right)
    {
        tmp[(*size)++] = '(';
    }
    _tree2str(root->right, n, size, tmp);
    if (root->right)
        tmp[(*size)++] = ')';
}


char* tree2str(struct TreeNode* root)
{
    int n = 20;
    int size = 0;
    int i = 0;
    char* tmp = (char*)malloc(sizeof(char) * n);
    for (; i < n; i++)
        tmp[i] = '\0';
    _tree2str(root, &n, &size, tmp);
    return tmp;
}


int main()
{
	char a[] = { "ABD##E##CF##G##" };
	int n = strlen(a);
	int pi = 0;


	BTNode*tmp=BinaryTreeCreate(a, n, &pi);
    
    struct TreeNode* n1 = sb5(1);
    struct TreeNode* n2 = sb5(2);
    struct TreeNode* n3 = sb5(3);
    struct TreeNode* n4 = sb5(4);
    struct TreeNode* n5 = sb5(5);
    struct TreeNode* n6 = sb5(6);
    struct TreeNode* n7 = sb5(1);
    struct TreeNode* n8 = sb5(2);
    struct TreeNode* n9 = sb5(1);
    struct TreeNode* n10 = sb5(0);
    struct TreeNode* n11= sb5(8);
    struct TreeNode* n12 = sb5(8);

    n1->right = n2;
    n2->right = n3;
    n3->right = n4;
    n4->right = n5;
    n5->right = n6;
    
    tree2str(n1);
    

	BinaryTreeLevelOrder(tmp);
	printf("\n");
	
	int xz=BinaryTreeComplete(tmp);

	printf("%d\n",BinaryTreeSize(tmp));

    printf("%d\n", BinaryTreeLeafSize(tmp));
	printf("%d\n", BinaryTreeLevelKSize(tmp,3));
	BTNode* cur=BinaryTreeFind(tmp, 'F');
	printf("%c\n", cur->data);

	BinaryTreeDestory(&tmp);

	return 0;
}